package com.dms.modules.product.controller;

import com.dms.modules.product.service.ProductAnalyticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 商品分析控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/product/analytics")
@Api(tags = "商品分析接口")
public class ProductAnalyticsController {

    @Autowired
    private ProductAnalyticsService productAnalyticsService;

    @GetMapping("/{productId}")
    @ApiOperation("获取商品分析数据")
    @PreAuthorize("hasAnyRole('ADMIN', 'MERCHANT')")
    public Map<String, Object> getProductAnalytics(@PathVariable Long productId) {
        log.info("获取商品分析数据，商品ID：{}", productId);
        return productAnalyticsService.getProductAnalytics(productId);
    }

    @GetMapping("/conversion/{productId}")
    @ApiOperation("获取商品转化率数据")
    @PreAuthorize("hasAnyRole('ADMIN', 'MERCHANT')")
    public Map<String, Object> getProductConversionRate(@PathVariable Long productId) {
        log.info("获取商品转化率数据，商品ID：{}", productId);
        return productAnalyticsService.getProductConversionRate(productId);
    }

    @GetMapping("/hot")
    @ApiOperation("获取热门商品排行")
    @PreAuthorize("hasAnyRole('ADMIN', 'MERCHANT')")
    public Map<String, Object> getHotProductsRanking(@RequestParam(defaultValue = "10") Integer limit) {
        log.info("获取热门商品排行，数量：{}", limit);
        return productAnalyticsService.getHotProductsRanking(limit);
    }
} 